package com.common.www.receiver;


import com.common.www.service.auth.TokenService;
import com.common.www.bean.entity.auth.Token;
import com.common.www.utils.TimeUtils;
import com.common.www.utils.TxtUtil;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;

@Component
@RabbitListener(queues = "com.design.www.token.queue")//监听的队列名称 TestDirectQueue
public class TokenDirectReceiver {
    @Autowired
    TokenService tokenService;

    @RabbitHandler
    public void process(HashMap<String, Object> hashMap) {
        if (hashMap == null)
            return;

        System.out.println("TokenDirectQueue消费者收到消息  : " + hashMap.toString());

        Token token = new Token();
        token.setCreateTime(null);
        token.setCdt(null);
        token.setLastUpdateTime(null);
        token.setLastUpdateTimetamp(null);
        token.setToken(hashMap.get("token") + "");
        Token res = tokenService.findOne(Example.of(token));
        if (res != null) {
            res.setLastUpdateTimetamp(TxtUtil.getLong(hashMap.get("datetamp") + ""));
            Date date = new Date();
            date.setTime(TimeUtils.getTimestamp(hashMap.get("date") + "", TimeUtils.YMD_HMS));
            res.setLastUpdateTime(date);
        }

        tokenService.save(res);

    }

}